﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Pampa.Common.Pages;
using Pampa.GenericEntity;
using Pampa.MVC.Fluent.Crud;
using Pampa.MVC.Fluent.CustomControls;
using Pampa.MVC.Fluent.Grid;
using Pampa.WebControls;

namespace Pampa.MVC.Fluent.Common
{
    public class PampaBuildersFactory
    {
        private static FloatingWindow _currentFloatingWindow;

        public MvcHtmlString CommonIncludes(string styleSheet = "Pampa.css")
        {
            var sb = new StringBuilder();

            sb.Append(IncludeStyle(styleSheet));

            sb.Append(IncludeLibrary("Pampa.Common/Common.js"));
            sb.Append(IncludeLibrary("Pampa.Common/Ajax.js"));
            sb.Append(IncludeLibrary("Pampa.Common/Url.js"));
            sb.Append(IncludeLibrary("Pampa.Common/TextButton.js"));

            sb.Append(IncludeLibrary("Pampa.WebControls/Drag.js"));
            sb.Append(IncludeLibrary("Pampa.WebControls/FloatingWindow.js"));
            sb.Append(IncludeLibrary("Pampa.WebControls/AlertWindow.js"));

            return new MvcHtmlString(sb.ToString());
        }

        public EntityViewBuilder EntityViewer(object entity)
        {
            return new EntityViewBuilder(entity);
        }

        public EntityViewBuilder EntityViewer<TEntity>()
        {
            return new EntityViewBuilder(typeof(TEntity));
        }

        public EntityEditBuilder EntityEditor(object entity)
        {
            return new EntityEditBuilder(entity);
        }

        public CustomGridBuilder<TEntity> EntityGrid<TEntity>()
        {
            return new CustomGridBuilder<TEntity>();
        }

        public CustomGridBuilder<GenericEntity.GenericEntity> ContentGrid()
        {
            var gridBuilder = new CustomGridBuilder<GenericEntity.GenericEntity>();
            
            var properties = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("pampa");
            string connectionName = properties["genericentity.connection.name"];

            if (string.IsNullOrEmpty(connectionName))
            {
                throw new Exception("genericentity.connection.name has to be defined");
            }

            gridBuilder.DataReader(new GenericEntityDataReader(connectionName));
            return gridBuilder;
        }

        public BasicPagerBuilder BasicPager()
        {
            return new BasicPagerBuilder();
        }

        public WindowPagerBuilder WindowPager()
        {
            return new WindowPagerBuilder();
        }

        public FloatingWindowBeginBuilder FloatingWindowBegin()
        {
            _currentFloatingWindow = new FloatingWindow();
            return new FloatingWindowBeginBuilder(_currentFloatingWindow);
        }

        public FloatingWindowEndBuilder FloatingWindowEnd()
        {
            var builder = new FloatingWindowEndBuilder(_currentFloatingWindow);
            _currentFloatingWindow = null;
            return builder;
        }

        #region Private Functions

        private string IncludeStyle(string url)
        {
            return "<link href=\"" + AbstractPage.GetResourceUrl(url) + "\" rel=\"stylesheet\" type=\"text/css\" />\n";
        }

        private string IncludeLibrary(string url)
        {
            return "<script src=\"" + AbstractPage.GetResourceUrl(url) + "\" type=\"text/javascript\"></script>\n";
        }

        #endregion
    }
}
